package meta.api.web.data.business;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import meta.api.web.common.business.BusiCmnService;
import meta.api.web.common.util.ApiUtil;
import meta.api.web.data.businessinfo.FolderDataInfo;
import meta.api.web.data.dao.DataMngDao;
import meta.api.web.data.presentation.form.FolderInfo;
import meta.api.web.directory.businessinfo.DirInfo;
import meta.common.exception.CmnBizException;
import meta.common.util.JsonUtil;
import meta.common.util.ListUtil;
import meta.common.util.StrUtil;

@Component
public class DataMngService {

    @Autowired
    private BusiCmnService busiCmnService;

    @Autowired
    private DataMngDao dataMngDao;

    public int getDataTotal(String userId, String collection, String type, String folder) {
        return dataMngDao.getDataTotal(userId, collection, type, folder);
    }

    public List<String> getDataList(String userId, String collection, String type, String folder,
            LinkedHashMap<String, Integer> projectionMap, Boolean projectionIncludeFlag,
            Integer current, Integer pageSize) {
        List<String> projectionList = null;
        if (projectionMap != null) {
            projectionList = new ArrayList<String>();
            projectionList.addAll(projectionMap.keySet());
        }
        return dataMngDao.getDataList(userId, collection, type, folder,
                projectionList, projectionIncludeFlag, current, pageSize);
    }

    public void saveData(String id, String userId, String collection, String data,
            String owner, String editor) {
        String keyVal = "{\"_id\":\"" + id + "\"}";
        dataMngDao.saveData(id, userId, collection, data, keyVal, owner, editor);
    }

    public void updateData(String id, String userId, String data, String editor) {
        dataMngDao.updateData(id, userId, data, editor);
    }

    public String getData(String id, String userId,
            LinkedHashMap<String, Integer> projectionMap, Boolean projectionIncludeFlag) {
        List<String> projectionList = null;
        if (projectionMap != null) {
            projectionList = new ArrayList<String>();
            projectionList.addAll(projectionMap.keySet());
        }
        return dataMngDao.getData(id, userId, projectionList, projectionIncludeFlag);
    }

    public void deleteData(String id, String userId) {
        dataMngDao.deleteData(id, userId);
    }

    public int getFolderListTotal(String userId, String type) {
        return dataMngDao.getFolderListTotal(userId, type);
    }

    public List<FolderInfo> getFolderList(String userId, String type) throws CmnBizException {
        List<FolderInfo> folderList = dataMngDao.getFolderList(userId, type);
        if (ListUtil.isEmptyList(folderList)) {
            return null;
        }
        List<FolderDataInfo> folderDataList = dataMngDao.getFolderDataList(userId, type);
        for (FolderInfo folder : folderList) {
            folder.setList(extractFolderDataList(folder.getName(), folderDataList));
        }
        return folderList;
    }

    public void updateFolder(String userId, String collection,
            String oldFolderName, String newFolderName,
            List<Object> dataList, String editor) {
        if (StrUtil.isNotEmpty(newFolderName)) {
            busiCmnService.updateDirInfo(userId, collection, oldFolderName, newFolderName);
        }
        if (ListUtil.isNotEmptyList(dataList)) {
            String folderName = StrUtil.isNotEmpty(newFolderName) ? newFolderName : oldFolderName;
            for (Object data : dataList) {
                String dataJsonStr = JsonUtil.toJsonStr(data);
                // 获取ID
                String dataId = ApiUtil.getKeyStrVal(dataJsonStr, "_id");
                if (StrUtil.isEmpty(dataId)) {
                    dataId = ApiUtil.getKeyStrVal(dataJsonStr, "id");
                }
                if (StrUtil.isNotEmpty(dataId)) {
                    dataMngDao.updateData(userId, collection, folderName, dataId, dataJsonStr, editor);
                }
            }
        }
    }

    public FolderInfo getFolder(String userId, String type, String fullpath) {
        DirInfo dir = busiCmnService.getDirInfo(userId, type, fullpath);
        if (dir == null) {
            return null;
        }
        FolderInfo result = new FolderInfo();
        result.setId(dir.getId());
        result.setUserid(dir.getUserid());
        result.setType(dir.getType());
        result.setName(dir.getFullpath());
        List<String> dataList = dataMngDao.getDataList(userId, type, fullpath);
        result.setList(dataList);
        return result;
    }

    private List<String> extractFolderDataList(String folderName, List<FolderDataInfo> folderDataList) {
        if (ListUtil.isEmptyList(folderDataList)) {
            return null;
        }
        List<String> result = new ArrayList<String>();
        for (FolderDataInfo folderData : folderDataList) {
            if (folderName.equals(folderData.getFolderName())) {
                result.add(folderData.getData());
            }
        }
        return result;
    }
}
